#! -*- coding:utf-8 -*-
import logging

from django.core.management.base import BaseCommand

from common.bankcard.db import get_bankcard_by_status
from common.bankcard.handler import hide_account_number
from common.bankcard.model import BANKCARD_STATUS
from common.cache import redis_cache
from common.utils.telegram import send

_LOGGER = logging.getLogger('worker')

# cd /home/ubuntu/af-env/hydra;/home/ubuntu/af-env/bin/python manage.py


class Command(BaseCommand):

    def handle(self, **options):
        enable_card = get_bankcard_by_status(BANKCARD_STATUS.ENABLE)

        for item in enable_card:
            data = item.as_dict()
            online_status = 1 if redis_cache.get_bankcard_online_status(data['name']) else 0
            if online_status == 0:
                card_type = u'Recharge,上分'
                if data['type'] == 1:
                    card_type = u'Withdraw,下分'
                telegram_header = "BankCard's Status Enable,But Online_status Is Offline"
                message = "Bank: " + (data['bank_en'] or data['bank']) + "\r\n" + \
                          " Card Code: " + data['name'] + "\r\n" + \
                          " Username: " + unicode(data['account_holder']) + "\r\n" + \
                          " Account No: " + hide_account_number(data['account_number']) + "\r\n" + \
                          " Status: Enable" + "\r\n" + \
                          " Online_status: Offline" + "\r\n" + \
                          " Card Type: " + card_type
                message = "*" + telegram_header + "* \r\n``` " + message + " ```"
                send(message, "status.host", data['type'])
